/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mamayukero.ui.environment;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import mamayukero.main.MainGUI;
import mamayukero.problem.environment.Environment;
import mamayukero.problem.environment.component.Vendor;
import mamayukero.problem.environment.map.Const;
import mamayukero.ui.*;

/**
 *
 * @author user1
 */
public class UIEnvironment extends JPanel {
    
  

    
    public UIEnvironment(int mwidth, int mheight)
    {
        xlimit = 10;
        ylimit = 25;
        mapwidth = mwidth-(2*xlimit); //dikurangi 10 supaya rapi
        mapheight = mheight-(2*ylimit); //well the same goes for this guy
        cols = Environment.map.col; //jumlah tetap, cuma penomoran kolom dan baris akan dimulai dari 0.
        rows = Environment.map.row;
        
        iconwidth = mapwidth/cols;
        iconheight = mapheight/rows;
       
        //dibuat persegi, toh tidak berlaku kalau width = height
            if(iconwidth < iconheight)
            {iconheight = iconwidth;}
            else if(iconheight < iconwidth)
            {iconwidth = iconheight;}
        //perbaiki xlimit, ylimit, mapwidth dan mapheight!
            xlimit = (mwidth - (iconwidth*cols))/2;
            ylimit = (mheight - (iconheight*rows))/2;
            mapwidth = mwidth-(2*xlimit);
            mapheight = mheight-(2*ylimit);
            
        isClicked = false;
        clickedLocation = new Point(0,0);

        initComponents();
       /* addMouseListener(new MouseAdapter() { 
          public void mousePressed(MouseEvent me) { 
            System.out.println(me); 
          } 
        });*/
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                 //periksa apakah mouse berada di area map
                if(e.getX()>= xlimit && e.getX() <= (mapwidth+xlimit) && e.getY() >= ylimit && e.getY() <= (mapheight+ylimit))
                {
                //hitung ia berada di kolom mana
                    clickedLocation.x = (e.getX()-xlimit)/iconwidth;
                    clickedLocation.y = (e.getY()-ylimit)/iconheight;
                    isClicked = true;
                    //System.out.println("saat ini mouse mengklik kolom ke "+colclicked+", row ke "+rowclicked);
                    
                }
                else
                {
                    isClicked = false;
                    //System.out.println("mouse berada diluar map");
                }
            MainGUI.updateInfo();
                  repaint(); 
            }
        });
        this.setFocusable(true);
    }
    
    private void initComponents()
    {
        
        try{
            //buat array of image
            /*             
             * LEGENDA
             * 0 = truk
             * 1 = blank
             * 2 = factory
             * 3 = vendor
             * 4 = wall
             * 5 = blank_clicked
             * 6 = factory_clicked
             * 7 = vendor_clicked
             * 8 = wall_clicked
             */
            images = new BufferedImage[9];
            dummy = ImageIO.read(new File(Const.TRUCK_PIC));
            images[0] = resize(dummy,iconwidth,iconheight);
            dummy = ImageIO.read(new File(Const.BLANK_PIC));
            images[1] = resize(dummy,iconwidth, iconheight);
            dummy = ImageIO.read(new File(Const.FACTORY_PIC));
            images[2] = resize(dummy,iconwidth,iconheight);
            dummy = ImageIO.read(new File(Const.VENDOR_PIC));
            images[3] = resize(dummy,iconwidth,iconheight);
            dummy = ImageIO.read(new File(Const.WALL_PIC));
            images[4] = resize(dummy,iconwidth,iconheight);
            dummy = ImageIO.read(new File(Const.BLANK_CLICK_PIC));
            images[5] = resize(dummy,iconwidth,iconheight);
            dummy = ImageIO.read(new File(Const.FACTORY_CLICK_PIC));
            images[6] = resize(dummy,iconwidth,iconheight);
            dummy = ImageIO.read(new File(Const.VENDOR_CLICK_PIC));
            images[7] = resize(dummy,iconwidth,iconheight);
            dummy = ImageIO.read(new File(Const.WALL_CLICK_PIC));
            images[8] = resize(dummy,iconwidth,iconheight);
        }
        catch(IOException e){}
        //mulai map generation
        generateMap();
        //pasang map panel
        repaint();
    }
    
    public void generateMap()
    //dipanggil untuk melakukan generate terhadap Map yang ada, mencatat lokasi wall, factory, dan vendor.
            // untuk lokasi truck, akan dilakukan pemeriksaan per step.
    {
      xMap = new int[cols][rows];
      //pencarian wall dan blank
       for(int j=0;j<rows;j++)
        {
            for(int i=0;i<cols;i++)
            {
                if(Environment.map.block[i][j] == true)
                {
                    xMap[i][j] = 1; //wall
                }
                else
                {
                    xMap[i][j] = 4; //blank
                }
            }
        }
       //penetapan lokasi factory lokasi factory
       xMap[Environment.map.factory.getLocation().x][Environment.map.factory.getLocation().y] = 2; //factory
       //penetapan lokasi vendor
       for(Vendor v:Environment.map.vendor){
            xMap[v.getLocation().x][v.getLocation().y] = 3; //vendor
        }
       
    }
    
    private static BufferedImage resize(BufferedImage image, int width, int height)
    {
        BufferedImage resImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resImage.createGraphics();
        g.drawImage(image,0,0,width,height,null);
        g.dispose();
        return resImage;
    }
    
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        int i,j;
        
        for(j=0;j<rows;j++)
        {
            for(i=0;i<cols;i++)
            {
               
                if(isClicked && i == clickedLocation.x && j == clickedLocation.y)
                {g2d.drawImage(images[xMap[i][j]+4], (i*iconwidth)+xlimit, (j*iconheight)+ylimit, null);}
                else
                {g2d.drawImage(images[xMap[i][j]], (i*iconwidth)+xlimit, (j*iconheight)+ylimit, null);}
                
                 //pengecekan, apakah truk ada disana atau tidak

                if(i == Environment.map.truck.location.x && j == Environment.map.truck.location.y)
                
                {
                    //System.out.println("found truck at "+i+","+j);
                    g2d.drawImage(images[0], (i*iconwidth)+xlimit, (j*iconheight)+ylimit, null);
                }
            }
        }
    }
    
    public static void main (String args[])
    {
        //new UIEnvironment();    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new UIEnvironment(500,500);
            }
        });
    }

    //dibawah ini list variabel, ditaruh di bawah supaya mudah dirubah :D 
    
    
    private int xlimit, ylimit;
    private int iconwidth,iconheight;
    private int cols,rows;
    private int mapwidth, mapheight;
    public static Point clickedLocation;
    public static boolean isClicked;
    public static  int[][] xMap;
    private BufferedImage dummy;
    private BufferedImage[] images;
    
    }
